package com.healthyeating.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.healthyeating.common.utils.DateUtils;
import com.healthyeating.common.utils.SecurityUtils;
import com.healthyeating.manage.domain.ShopList;
import com.healthyeating.manage.domain.dto.ShopListProductDto;
import com.healthyeating.manage.mapper.ShopListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.healthyeating.manage.mapper.ProductMapper;
import com.healthyeating.manage.domain.Product;
import com.healthyeating.manage.service.IProductService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 清单详情Service业务层处理
 *
 * @author 段奇乐
 * @date 2024-11-26
 */
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ShopListMapper shopListMapper;

    /**
     * 查询清单详情
     *
     * @param productId 清单详情主键
     * @return 清单详情
     */
    @Override
    public Product selectProductByProductId(Long productId) {
        return productMapper.selectProductByProductId(productId);
    }

    /**
     * 查询清单详情列表
     *
     * @param product 清单详情
     * @return 清单详情
     */
    @Override
    public List<Product> selectProductList(Product product) {
        product.setUserId(SecurityUtils.getUserId());
        return productMapper.selectProductList(product);
    }

    /**
     * 新增清单详情
     *
     * @param product 清单详情
     * @return 结果
     */
    @Override
    public int insertProduct(Product product) {
        product.setStatus(0);
        ShopList shopList = shopListMapper.selectShopListByListId(product.getShopListId());
        product.setListName(shopList.getListName());
        product.setUserId(SecurityUtils.getUserId());
        product.setCreateTime(DateUtils.getNowDate());
        return productMapper.insertProduct(product);
    }

    /**
     * 修改清单详情
     *
     * @param product 清单详情
     * @return 结果
     */
    @Override
    public int updateProduct(Product product) {
        int result = productMapper.updateProduct(product);
        if (product.getStatus() == 1) {
            // 判断此次修改的商品是否为清单里的最后一个完成的
            List<Product> products = productMapper.selectProductByListId(product.getShopListId());
            // 如果每个商品的status都为1，说明都已完成，修改清单状态为已完成
            if (products.stream().allMatch(product1 -> product1.getStatus() == 1)) {
                ShopListProductDto shopList = new ShopListProductDto();
                shopList.setListId(product.getShopListId());
                shopList.setStatus(1);
                shopListMapper.updateShopList(shopList);
            }
        }
        return result;
    }

    /**
     * 批量完成清单详情
     *
     * @param productIds 需要修改的清单详情主键集合
     * @return 结果
     */
    @Override
    @Transactional
    public int updateProductByProductIds(Long[] productIds) {
        List<Product> productList = new ArrayList<>();
        for (Long productId : productIds) {
            Product product = productMapper.selectProductByProductId(productId);
            if (product.getStatus().equals(1)) {
                continue;
            }
            product.setStatus(1);
            productList.add(product);
        }
        int result = productMapper.batchUpdateProduct(productList);
        // 修改清单状态为已完成
        productList.forEach(productItem -> {
            List<Product> products = productMapper.selectProductByListId(productItem.getShopListId());
            if (products.stream().allMatch(product1 -> product1.getStatus() == 1)) {
                ShopListProductDto shopList = new ShopListProductDto();
                shopList.setListId(productItem.getShopListId());
                shopList.setStatus(1);
                shopListMapper.updateShopList(shopList);
            }
        });
        return result;
    }

    /**
     * 批量删除清单详情
     *
     * @param productIds 需要删除的清单详情主键
     * @return 结果
     */
    @Override
    public int deleteProductByProductIds(Long[] productIds) {
        return productMapper.deleteProductByProductIds(productIds);
    }

    /**
     * 删除清单详情信息
     *
     * @param productId 清单详情主键
     * @return 结果
     */
    @Override
    public int deleteProductByProductId(Long productId) {
        return productMapper.deleteProductByProductId(productId);
    }
}
